interface INameInfo {
    firstName: string,
    middleName?: string, // 可选属性
    lastName: string,
    [prop: string]: any, /// 接收余下参数，传递多余参数可以避免报错
    readonly isAdmin: boolean // 只读属性
}

const getFullName = ({ firstName, lastName }: INameInfo): string => {
    return firstName + lastName
}

getFullName({ firstName: 'a', lastName: 'b', age: 33, isAdmin: false })
// 使用类型断言传递多余参数避免报错
getFullName({ firstName: 'a', lastName: 'b', age: 33, isAdmin: false } as INameInfo) // 类型断言

// 使用类型兼容性，传递多余参数避免报错
const nameParms = { firstName: 'a', lastName: 'b', age: 33, isAdmin: false }
getFullName(nameParms)

/**
 * 只读数组
 */
interface ArrInter {
    0: number,
    readonly 1: string
}

const arrInter: ArrInter = [1, "1"]
arrInter[0] = 3
// arrInter[1] = "3"

/**
 * 通过接口定义函数
 */

// interface AddFunc {
//     (num: number, num2: number): number
// }

// 上面的定义，TS建议改为类型别名的方式定义
type AddFunc = (num: number, num2: number) => number

/**
 * 通过接口定义索引名 | 只能是string或number
 */
interface RoleDic {
    [id: number]: string
}
const role1: RoleDic = {
    1: "123",
    "2": "123",
    // "a": "123" // 报错
}
interface RoleName {
    [name: string]: string
}
const role2: RoleName = {
    1: "123",
    "2": "123",
    "a": "123"
}

/**
 * 接口的继承
 */
interface ITyre {
    tyreCount: number
}
interface IBike extends ITyre {
    color: string
}
interface ICar extends ITyre {
    color: "red" // 必须是红色
}
const bike: IBike = {
    tyreCount: 2,
    color: "white"
}
const car: ICar = {
    tyreCount: 2,
    color: "red"
}

/**
 * 混合类型接口
 */

interface Counter {
    (): void,
    count: number,
    test(): void,
}

const getCounter = (): Counter => {
    const c = () => { c.count++ }
    c.count = 0
    c.test = () => {
        console.log('test');
    }
    return c
}
const counter = getCounter()

counter()
counter()
counter.test()
console.dir(counter)
console.dir(counter.count)